package design.pattern.Lu10ChainResponsibilityPattern;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @program JavaBase
 * @description: 责任链模式
 * @author: zhanglu
 * @create: 2020-04-07 22:51:00
 */
public class Lu10ChainResponsibilityPattern {

    public static void main(String[] args) {
        Random random = new Random();

        List<IWomen01> women01List = new ArrayList<>();

        for (int i = 0; i < 1; i++) {
            women01List.add(new Women01(random.nextInt(4), "去逛街"));
        }

        Handler father = new Father01();
        Handler husband = new Husband01();
        Handler son = new Son01();
        father.setNext(husband);
        husband.setNext(son);

        for (IWomen01 women01 : women01List) {
            father.handlerMessage(women01);
        }

    }

}

interface IWomen01{
    int getType();

    String getRequest();
}

class Women01 implements IWomen01{

    private int type;
    private String request;


    public Women01(int type, String request){
        this.type = type;
        if(type == 1){
            this.request = "女儿的请示是：" + request;
        }else if(type == 2){
            this.request = "妻子的请示是：" + request;
        }else if(this.type == 3){
            this.request = "母亲的请示是：" + request;
        }
    }

    @Override
    public int getType() {
        return this.type;
    }

    @Override
    public String getRequest() {
        return this.request;
    }
}

abstract class Handler{

    public final static int FATHER_LEVEL_REQUEST = 1;
    public final static int HUSBAND_LEVEL_REQUEST = 2;
    public final static int SON_LEVEL_REQUEST = 3;

    private int level;
    private Handler nextHandler;

    public Handler(int level){
        this.level = level;
    }

    public final void handlerMessage(IWomen01 women01){
        if(women01.getType() == this.level){
            this.response(women01);
        }else {
            if(this.nextHandler != null){
                this.nextHandler.handlerMessage(women01);
            }else {
                System.out.println("---没地方请示了，按不同意处理---");
            }
        }
    }

    abstract void response(IWomen01 iWomen01);

    public void setNext(Handler handler){
        this.nextHandler = handler;
    }

}

class Father01 extends Handler{

    public Father01() {
        super(Handler.FATHER_LEVEL_REQUEST);
    }

    @Override
    void response(IWomen01 iWomen01) {
        System.out.println("--------女儿向父亲请示-------");
        System.out.println(iWomen01.getRequest());
        System.out.println("父亲的答复是:同意\n");
    }

}

class Husband01 extends Handler{
    public Husband01() {
        super(Handler.HUSBAND_LEVEL_REQUEST);
    }

    @Override
    void response(IWomen01 iWomen01) {
        System.out.println("--------妻子向丈夫请示-------");
        System.out.println(iWomen01.getRequest());
        System.out.println("丈夫的答复是:同意\n");
    }
}

class Son01 extends Handler{
    public Son01() {
        super(Handler.SON_LEVEL_REQUEST);
    }

    @Override
    void response(IWomen01 iWomen01) {
        System.out.println("--------母亲向儿子请示-------");
        System.out.println(iWomen01.getRequest());
        System.out.println("儿子的答复是:同意\n");
    }
}

